package com.cloud.custom.api.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.custom.api.entity.CustomBizConfig;
import com.cloud.custom.api.entity.CustomBizFields;
import com.cloud.custom.api.service.CustomBizConfigService;
import com.cloud.custom.common.exception.BizException;
import com.cloud.custom.common.result.QkIcon;
import com.cloud.custom.common.result.QkTreeNode;
import com.cloud.custom.common.result.Quokka;
import com.cloud.custom.common.utils.TreeHelper;
import com.cloud.custom.interfaces.FeignClientForm;
import com.cloud.custom.interfaces.FeignClientFreeflow;

@RestController
@RequestMapping("custom/api/config")
public class CustomBizConfigController {
	@Autowired
	private FeignClientFreeflow feignClientFreeflow;

	@Autowired
	private FeignClientForm feignClientForm;

	@Autowired
	private CustomBizConfigService customBizConfigService;

	/**
	 * 加载自定义业务树
	 * 
	 * @return
	 */
	@RequestMapping("listcustombiztree")
	public Quokka listCustomBizTree() {
		List<CustomBizConfig> bizTypes = customBizConfigService.listCustomBizs();
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>(bizTypes.size() + 1);
		if (null != bizTypes && bizTypes.size() > 0) {
			QkTreeNode rootNode = new QkTreeNode("OL", "自定义业务");
			rootNode.setRootFlag(true);
			rootNode.setExpanded(true);
			rootNode.setExpandedIcon(QkIcon.FA_FOLDER_OPEN);
			rootNode.setSelectable(false);
			nodes.add(rootNode);
			for (CustomBizConfig biz : bizTypes) {
				QkTreeNode node = new QkTreeNode(biz.getBizId(), biz.getBizName());
				node.setSeniorId("OL");
				node.setSelectable(true);
				node.setIcon(QkIcon.FA_LAPTOP);
				node.setSelectable(true);
				node.setLeaf(true);
				Map<String, Object> data = new HashMap<String, Object>(2);
				data.put("procDefId", biz.getProcDefId());
				data.put("formDefId", biz.getFormDefId());
				node.setData(data);
				nodes.add(node);
			}
		}
		return new Quokka(true, TreeHelper.buildList2Tree(nodes));
	}

	/**
	 * 新增自定义业务
	 * 
	 * @return
	 */
	@RequestMapping("addcustombiz")
	public Quokka addCustomBiz(@RequestBody CustomBizConfig biz) {
		if (null == biz.getBizId() || biz.getBizId() <= 100) {
			throw new BizException(500, "0到100的业务编码是为标准流程预留，请填写一个大于100的业务编码");
		}
		if(customBizConfigService.existsBizId(biz.getBizId())){
			throw new BizException(500, "业务编码已存在，请更换业务编码");
		}
		long procDefId = feignClientFreeflow.addCustomBiz(biz.getBizId(), biz.getBizName());
		if(procDefId == 0){
			throw new BizException(500, "创建流程出错，请检查流程服务是否启动");
		}
		biz.setProcDefId(procDefId);
		long formDefId = feignClientForm.addCustomForm(biz.getBizId(), biz.getBizName(), biz.getProcDefId());
		biz.setFormDefId(formDefId);
		return new Quokka(true, customBizConfigService.addCustomBiz(biz));
	}

	/**
	 * 删除自定义业务信息
	 * 
	 * @param bizId
	 * @return
	 */
	@RequestMapping("deletecustombiz")
	public Quokka deleteCustomBiz(Integer bizId) {
		customBizConfigService.deleteCustomBiz(bizId);
		return new Quokka(true);
	}
	
	/**
	 * 加载业务表信息
	 * @param tableName
	 * @return
	 */
	@RequestMapping("loadcustomtables")
	public Quokka loadCustomTables(){
		return new Quokka(true, customBizConfigService.loadCustomTable());
	}
	
	/**
	 * 加载业务表字段信息
	 * @param tableName
	 * @return
	 */
	@RequestMapping("loadcustomtablefields")
	public Quokka loadCustomTableFiles(String tableName, Integer bizId){
		return new Quokka(true, customBizConfigService.loadCustomTableFiles(tableName, bizId));
	}
	/**
	 * 保存业务表相关字段
	 * @param fields
	 * @return
	 */
	@RequestMapping("savecustomfields")
	public Quokka saveCustomFields(Integer bizId, String tableName, @RequestBody List<CustomBizFields> fields){
		customBizConfigService.saveCustomFields(bizId, tableName, fields);
		return new Quokka(true);
	}
	/**
	 * 加载配置信息
	 * @param bizId
	 * @return
	 */
	@RequestMapping("load")
	public Quokka load(Integer bizId){
		return new Quokka(true, customBizConfigService.load(bizId));
	}
	
	/**
	 * 加载字段信息
	 * @param bizId
	 * @return
	 */
	@RequestMapping("loadfieldname")
	public Quokka loadFieldName(Integer bizId){
		return new Quokka(true, customBizConfigService.loadFieldName(bizId));
	}
}
